package com.nanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanhang.booksearch.BookS0;
import com.nanhang.entity.Bookborrow;
import com.nanhang.entity.Bookinfo;
import com.nanhang.mapper.BookborrowMapper;
import com.nanhang.service.IBookborrowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.service.IBookinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.PageResult;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ymm
 * @since 2025-01-09
 */
@Service
public class BookborrowServiceImpl extends ServiceImpl<BookborrowMapper, Bookborrow> implements IBookborrowService {

    @Autowired
    private IBookinfoService bookinfoService;

    @Override
    public PageResult<Bookborrow> search(Integer page, Integer pagesize, BookS0 BS0){
        QueryWrapper<Bookborrow> re =new QueryWrapper<>();
        re.like(StrUtil.isNotBlank(BS0.getRname()),"rname",BS0.getRname());
        re.like(StrUtil.isNotBlank(BS0.getStime()),"stime",BS0.getStime());
        re.eq(BS0.getIsbn() != null && BS0.getIsbn() != 0,"isbn",BS0.getIsbn());

        Page<Bookborrow> repage = this.page(new Page<>(page,pagesize),re);

        repage.getRecords().forEach(r->{
            Bookinfo book = bookinfoService.getById(r.getIsbn());
            r.setBname(book.getBname());
        });

        return new PageResult<>(repage.getRecords(),repage.getTotal());
    }
}
